package com.wf.controller;

import com.aliyun.com.viapi.FileUtils;
import com.aliyuncs.exceptions.ClientException;
import com.wf.constant.MessageConstant;
import com.wf.exception.BankException;
import com.wf.properties.AliYunProperties;
import com.wf.util.AliOssUtil;
import com.wf.util.FileNameUtils;
import com.wf.util.IdentificationUtil;
import com.wf.utils.RedisUtil;
import com.wf.web.base.BaseController;
import com.wf.web.domain.Result;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * 文件上传接口
 * 处理文件上传相关请求
 */
@Slf4j
@RestController
@RequestMapping("/upload")
@CrossOrigin
public class UpLoadController extends BaseController {

    @Autowired
    AliOssUtil aliOssUtil;

    @Autowired
    IdentificationUtil identificationUtil;

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    AliYunProperties aliYunProperties;

    /**
     * 上传文件
     *
     * @param file 待上传的文件
     * @return 文件的访问URL
     */
    @PostMapping("/file")
    public Result upload(@RequestPart MultipartFile file) throws BankException {

        // 生成新的文件名
        String objectName = FileNameUtils.getFileName(file.getOriginalFilename());

        try {
            // 调用aliOssUtil的upload方法上传文件，并获取上传后的文件路径
//            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            FileUtils fileUtils = FileUtils.getInstance(aliYunProperties.getAccessKeyId(), aliYunProperties.getAccessKeySecret());

            String filePath = fileUtils.upload(objectName, file.getInputStream());

            // 返回成功的结果，包含文件路径
            return Result.success(filePath);
        } catch (IOException e) {
            // 如果发生IO异常，记录错误日志
            log.error(e + "");
            throw new BankException(MessageConstant.UPLOAD_FAILED);
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }

    }


        /**
         * 身份证识别
         *
         * @param file       待上传的文件
         * @param idCardSide 文件类型
         * @return
         * @throws IOException
         */
    @PostMapping("/uploadIdCard")
    public Result uploadIdCard(@RequestPart MultipartFile file, @RequestParam String idCardSide) throws IOException, BankException {

//        User user = new User();
//        user.setUserId(BaseContext.getCurrentId());
//        Map<String, Object> userInfo = identificationUtil.getUserInfo(file.getBytes(), idCardSide);
//
//        if (StrUtil.equals(idCardSide, "front")) {
//            Result upload = upload(file);
//            userInfo.put("idCardFront", upload.getData());
//            BaseContext.removeCurrentId();
//            return updateUser(userInfo);
//
//        } else {
//            Result upload = upload(file);
//            String uploadData = (String) upload.getData();
//            user.setIdCardBack(uploadData);
//            userService.updateById(user);
//            BaseContext.removeCurrentId();
//            return Result.success();
//        }
        return Result.success();
    
    }

    @NotNull
    private Result updateUser(Map<String, Object> userInfo) {
//        User user = new User();
//        String idCardFront = (String) userInfo.get("idCardFront");
//        user.setUserId(BaseContext.getCurrentId());
//        user.setRealName((String) userInfo.get("realName"));
//        user.setIdCard((String) userInfo.get("idCard"));
//        user.setSex((String) userInfo.get("gender"));
//        user.setIdCardFront(idCardFront);
//        user.setIdCardType("身份证");
//        redisUtil.set(RedisConstant.APARTMENT_USED_IMG, idCardFront);
//        userService.updateById(user);

        return Result.success();
    }



}
